package com.example.zg.learn.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.zg.learn.R;
import com.example.zg.learn.adapter.BaseRecyclerViewAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述：
 * zhougan
 * 2019/1/18
 **/
public class BookListFragment extends BaseFragment {
    private static final String TAG = "BookListFragment>>>";
    private SwipeRefreshLayout mSwipeRefreshLayout;
    private RecyclerView mRecyclerView;
    private ArrayList<String> books;
    private Adapter mAdapter;
    //模拟网络获取数据
    private int pagerNum=1;//第一页
    private int pagerSize=30;//每页显示的数目

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(savedInstanceState!=null){
            Log.i(TAG, "onCreate: 重建");
            pagerNum=savedInstanceState.getInt("pagerNum");
            books=savedInstanceState.getStringArrayList("books");
        } else{
            Log.i(TAG, "onCreate: 新建");
        }
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putStringArrayList("books",books);
        outState.putInt("pagerNum",pagerNum);

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_booklist,container,false);

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }

    private Handler bookHandler = new Handler(msg -> {
        if(mAdapter!=null){
            mAdapter.notifyDataSetChanged();
        }
        if(mSwipeRefreshLayout!=null && mSwipeRefreshLayout.isRefreshing()){
            mSwipeRefreshLayout.setRefreshing(false);
        }
        return true;
    });



    private void getBooks(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if(books==null){
                    books=new ArrayList<>();
                }else{
                    books.clear();
                }
                int start=(pagerNum-1)*pagerSize;
                int end=pagerNum*pagerSize;
                List<String> list=new ArrayList<>();
                for(int i=start;i<end;i++){
                    list.add("book"+i);
                }
                books.addAll(list);
                pagerNum++;
                bookHandler.sendEmptyMessage(1);
            }
        }).start();
    }




    private void initView(View view){
        if(books==null){
            books=new ArrayList<>();
            getBooks();
        }


        mSwipeRefreshLayout=view.findViewById(R.id.swipeRefreshLayout);
        mRecyclerView=view.findViewById(R.id.recyclerView);

        mSwipeRefreshLayout.setOnRefreshListener(this::getBooks);

        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), OrientationHelper.VERTICAL,false));

        mAdapter=new Adapter();
        mAdapter.setData(books);
        mRecyclerView.setAdapter(mAdapter);


    }

    private class Adapter extends BaseRecyclerViewAdapter<String>{

        @Override
        public int getLayoutId(int viewType) {
            return R.layout.book_list_item;
        }

        @Override
        public void onBindData(VH holder, String data, int position, int viewType) {
            holder.setText(R.id.book_name,data);
        }
    }

}
